﻿import requests
import json
from urllib import parse

KEY = '9dbd1061a6a77951e0d8715b4c28aca6'



def get_response(msg):
    global KEY
    apiUrl = 'http://apis.juhe.cn/cook/query.php'
    data = {
        "menu": msg,
        'key'    : KEY,
        "dtype" : "json",
        "pn": "",  # 数据返回起始下标
        "rn": "",  # 数据返回条数，最大30
        "albums": "",  # albums字段类型，1字符串，默认数组
    }
    data= parse.urlencode(data)
    print(data)
    try:
        r = requests.get('{}?{}'.format(apiUrl,data))
        return json.loads(r.text)
    except:
        return




def get_url(data):
    url='<div class="column">'
    for i in data:
        url+='<div class="column">'
        url+='<div class="item">{}</div>'.format(i['title'])
        url += '<div class="item">{}</div>'.format(i['burden'])
        for j in i['steps']:
            url +='<div class="item">{}</div>'.format(j['step'])
            url += '<div class="item"><img src="{}" width="300"></div>'.format(j['step'])
        url+='</div>'
    url+='</div>'
    return url

r=get_response('红烧肉')['result']['data']

print(get_url(r))